package com.example.studentsRoom.controller;


import com.example.studentsRoom.entity.Absent;
import com.example.studentsRoom.form.SearchForm;
import com.example.studentsRoom.service.impl.AbsentService;
import com.example.studentsRoom.util.ResultUtil;
import com.example.studentsRoom.vo.PageVo;
import com.example.studentsRoom.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/absent")
public class AbsentController {

    @Autowired
    private AbsentService absentService;

    @GetMapping("/list/{page}/{size}")
    public ResultVo list(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
    ){
        PageVo pageVo = absentService.listPage(page, size);
        return ResultUtil.success(pageVo);
    }

    @GetMapping("/search")
    public ResultVo search(SearchForm form){
        PageVo search = absentService.search(form);
        return ResultUtil.success(search);
    }

    @GetMapping("/save")
    public ResultVo save(Absent absent){
        boolean save = absentService.save(absent);
        if (save) return ResultUtil.success(null);
        return ResultUtil.failed();
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id){
        boolean b = absentService.removeById(id);
        if (!b) return ResultUtil.failed();
        return ResultUtil.success(null);
    }
}

